<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="请求编码" prop="requestCode">
        <el-input v-model="formData.requestCode" placeholder="请输入请求编码" />
      </el-form-item>
      <el-form-item label="服务编码" prop="serviceCode">
        <el-input v-model="formData.serviceCode" placeholder="请输入服务编码" />
      </el-form-item>
      <el-form-item label="接口编码" prop="interfaceCode">
        <el-input
          v-model="formData.interfaceCode"
          placeholder="请输入接口编码"
        />
      </el-form-item>
      <el-form-item label="调度编码" prop="dispatchCode">
        <el-input
          v-model="formData.dispatchCode"
          placeholder="请输入调度编码"
        />
      </el-form-item>
      <el-form-item label="接口url" prop="interfaceUrl">
        <el-input v-model="formData.interfaceUrl" placeholder="请输入接口url" />
      </el-form-item>
      <el-form-item label="请求时间" prop="requestTime">
        <el-input v-model="formData.requestTime" placeholder="请输入请求时间" />
      </el-form-item>
      <el-form-item label="请求消息体" prop="requestBody">
        <el-input
          v-model="formData.requestBody"
          placeholder="请输入请求消息体"
        />
      </el-form-item>
      <el-form-item label="响应时间" prop="responseTime">
        <el-input
          v-model="formData.responseTime"
          placeholder="请输入响应时间"
        />
      </el-form-item>
      <el-form-item label="响应状态" prop="responseStatus">
        <el-input
          v-model="formData.responseStatus"
          placeholder="请输入响应状态"
        />
      </el-form-item>
      <el-form-item label="响应体" prop="responseBody">
        <el-input v-model="formData.responseBody" placeholder="请输入响应体" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "DEEXCHANGEDILOGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  requestCode: [
    { required: true, message: "请输入请求编码", trigger: "blur" },
    { min: 1, max: 45, message: "长度在 1 到 45 个字符", trigger: "blur" },
  ],
  serviceCode: [
    { required: true, message: "请输入服务编码", trigger: "blur" },
    { min: 1, max: 45, message: "长度在 1 到 45 个字符", trigger: "blur" },
  ],
  interfaceCode: [
    { required: true, message: "请输入接口编码", trigger: "blur" },
  ],
  dispatchCode: [
    { required: true, message: "请输入调度编码", trigger: "blur" },
  ],
  interfaceUrl: [{ required: true, message: "请输入接口url", trigger: "blur" }],
  requestTime: [{ required: true, message: "请输入请求时间", trigger: "blur" }],
  requestBody: [
    { required: true, message: "请输入请求消息体", trigger: "blur" },
  ],
  responseTime: [
    { required: true, message: "请输入响应时间", trigger: "blur" },
  ],
  responseStatus: [
    { required: true, message: "请输入响应状态", trigger: "blur" },
  ],
  responseBody: [{ required: true, message: "请输入响应体", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  requestCode: "",
  serviceCode: "",
  interfaceCode: "",
  dispatchCode: "",
  interfaceUrl: "",
  requestTime: "",
  requestBody: "",
  responseTime: "",
  responseStatus: "",
  responseBody: "",
  createTime: "",
  createdUserId: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        version,
        requestCode,
        serviceCode,
        interfaceCode,
        dispatchCode,
        interfaceUrl,
        requestTime,
        requestBody,
        responseTime,
        responseStatus,
        responseBody,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        requestCode,
        serviceCode,
        interfaceCode,
        dispatchCode,
        interfaceUrl,
        requestTime,
        requestBody,
        responseTime,
        responseStatus,
        responseBody,
        createTime,
        createdUserId,
        lastUpdateTime,
        lastUpdatedUserId,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.requestCode = "";
  formData.serviceCode = "";
  formData.interfaceCode = "";
  formData.dispatchCode = "";
  formData.interfaceUrl = "";
  formData.requestTime = "";
  formData.requestBody = "";
  formData.responseTime = "";
  formData.responseStatus = "";
  formData.responseBody = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
}

const loading = ref(false);

/** 数据接口调用日志保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dataexchange/deexchangedilog/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table");
    }
  });
}

/** 数据接口调用日志查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/dataexchange/deexchangedilog/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
